var app = getApp();
Page({
  data: {
    // input默认是1 
    num: 1,
    apiurl: app.data.apiurl,
    gid:'',
    gfare:'',
    spec:'',
    price:'',
    gtitle:'',
    ausername:'',
    amobile:'',
    area:'',
    addresss:'',
    test:{},
    openid: app.globalData.openid,
    // 使用data数据对象设置样式名 
    minusStatus: 'disabled',
    allprice:''
  },
  onLoad: function (options) {
    var that=this
    that.setData({
      gid:options.gid,
      num:options.num,
      spec:options.spec,
      price: options.price,
      allPrice:options.price * options.num
    });
    // console.log('0000' + options.gid);
    // console.log('0000' + options.num);
    // console.log('0000' + options.spec);
    // console.log('0000' + options.price);
    wx.setNavigationBarTitle({
      title: '订单详情'
    });
    //商品详情
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/getgoods',
      data: { gid: options.gid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        // console.log(res.data.data);
        that.setData({
          goods: res.data,
          gfare: res.data.data[0]['gfare'],
          gtitle: res.data.data[0]['gtitle']
        });
      }
    });
    //获取用户地址
    wx.chooseAddress({

      success: function (res) {
        console.log(res);

        that.setData({
          addobj: res,
          // address: res.data,
          ausername: res.userName,
          amobile: res.telNumber,
          area: res.provinceName + res.cityName + res.countyName,
          addresss: res.detailInfo
        });


      }
    });

 
  },
  getAddress: function () {
    var that = this
    wx.chooseAddress({

      success: function (res) {
        console.log(res);

        that.setData({
          addobj: res,
          // address: res.data,
          ausername: res.userName,
          amobile: res.telNumber,
          area: res.provinceName + res.cityName + res.countyName,
          addresss: res.detailInfo
        });


      }
    });
  },
  paymoney:function(e){
    var that = this;
  
    console.log(this.data.address);
    //添加立即购买订单
    wx.request({
      url: getApp().data.apiurl + '/index.php/index/index/addbuynow?gid=' + this.data.gid + '&spec=' + this.data.spec + '&price=' + this.data.price + '&onum=' + this.data.num + '&gtitle=' + this.data.gtitle + '&openid=' + app.globalData.openid + '&ofare=' + this.data.gfare+ '&ousername=' + this.data.ausername + '&omobile=' + this.data.amobile + '&area=' + this.data.area + '&oaddress=' + this.data.addresss,
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       
        //获得订单信息
        wx.request({
          url: app.data.apiurl + '/index.php/index/index/selectgoodsorders',
          data: {
            oid: res.data,
            openid:app.globalData.openid
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (resa) {
            that.goPay(resa);
          },
        });
      }
    });
  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减 
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus,
      allPrice: this.data.price * num,
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1 
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus,
      allPrice: this.data.price *num,
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回 
    this.setData({
      num: num
    });
  },

  goPay:function(e){
    //支付
    wx.request({
      url: app.data.apiurl + "/wxpay/wxpay.php?out_trade_no=" + e.data.data['order_sn'] + '&total_fee=' + e.data.data['opaymoney'] + '&openid=' + app.globalData.openid,
      data: {},
      method: 'POST',
      success: function (res) {
        console.log(res);
        console.log('unified order success, response is:', res)
        var payargs = res.data;
        wx.requestPayment({
          timeStamp: payargs.timeStamp,
          nonceStr: payargs.nonceStr,
          package: payargs.package,
          signType: payargs.signType,
          paySign: payargs.paySign,
          'success': function () {
            wx.switchTab({
              url: '../mine/mine',
            });

          },
          'fail': function () {
            wx.showToast({
              title: '用户取消支付',
              icon: 'clear',
              duration: 5000,
              image: '../../images/tanhao.png',
              success: function () {
                wx.switchTab({
                  url: '../mine/mine',
                });
              }
            });
          }
        })
      },
    });
  } 






})